package com.hushow.plugin.brick.service;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.plugin.core.PluginRegistry;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PaymentPluginRegistryManager implements InitializingBean {

    @Resource
    private PluginRegistry<PaymentPlugin, String> pluginRegistry;

    private List<PaymentPlugin> plugins;

    @Override
    public void afterPropertiesSet() throws Exception {
        this.plugins = this.pluginRegistry.getPlugins();
    }

    public PaymentPlugin getPlugin(String method) {
        for (PaymentPlugin plugin : plugins) {
            if (plugin.supports(method)) {
                return plugin;
            }
        }
        throw new IllegalArgumentException("Unsupported payment method: " + method);
    }
}
